package com.yfp.operation.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.operation.domain.WorkOrderDO;
import com.yfp.operation.mapper.WorkOrderMapper;
import com.yfp.operation.service.WorkOrderService;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @description 针对表【work_order(工单表)】的数据库操作Service实现
 * @createDate 2024-04-22 17:21:49
 */
@Service
public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrderDO> implements WorkOrderService {

    @Resource
    private WorkOrderMapper workOrderMapper;
    /**
     * 根据工单id查询工单信息
     *
     * @param orderIds 订单id列表
     * @return
     */
    @Override
    public List<WorkOrderDO> listByOrderIds(List<String> orderIds) {
        LambdaQueryWrapper<WorkOrderDO> wrapper = Wrappers.lambdaQuery(WorkOrderDO.class).in(WorkOrderDO::getOrderSn, orderIds);
        return this.list(wrapper);
    }

    /**
     * 获取今日进行中的工单
     *
     * @return
     */
    @Override
    public List<WorkOrderDO> listTodayInProcess() {
        return workOrderMapper.listToday("2");
    }

    /**
     * 获取今日待处理的工单
     *
     * @return
     */
    @Override
    public List<WorkOrderDO> listTodayPending() {
        return workOrderMapper.listToday("1");
    }

    @Override
    public WorkOrderDO getCompleted(WorkOrderDO workOrderDO) {
        LambdaQueryWrapper<WorkOrderDO> query = Wrappers.lambdaQuery(WorkOrderDO.class);
        query.eq(WorkOrderDO::getOrderSn, workOrderDO.getOrderSn());
        query.eq(WorkOrderDO::getComboId, workOrderDO.getComboId());
        query.eq(WorkOrderDO::getMachineId, workOrderDO.getMachineId());
        query.eq(WorkOrderDO::getWorkerOrderStatus, "3");
        List<WorkOrderDO> list = this.list(query);
        list.sort(Comparator.comparing(WorkOrderDO::getWorkOrderNo));
        return CollUtil.getFirst(list);
    }
}




